<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>06_列表渲染_过滤与排序</title>
</head>
<body>
    <div id="test">
        <input type="text">
        <ul>
            <li v-for="(p,index) in filterpersons" :key="index">
                {{index}}---{{p.name}}---{{p.age}}
            </li>
        </ul>

        <button @click="setOrderType(1)">年龄升序</button>
        <button @click="setOrderType(2)">年龄降序</button>
        <button @click="setOrderType(0)">原本顺序</button>

    </div>
    <script type="text/javascript" src="../js/vue.js"></script>
    <script type="text/javascript">
        new Vue({
            el: '#test',
            data:{
                searchName: '',
                orderTypt:0,//0代表原本，1代表升序，2代表降序
                persons:[
                    {name:'Tom',age:18},
                    {name:'Jack',age:16},
                    {name:'Bob',age:19},
                    {name:'Rose',age:17}
                ],
            },
            computed:{
                filterPersons(){
                    //取出相关的数据
                    const {searchName,persons} = this
                    //最终需要显示的数组
                    let fPersons;
                    //对persons进行过滤
                    fPerson = persons.filter(p => p.name.indexof(searchName)!==-1)
                    //排序
                    if(orderType!==0){}
                    fPersons.sort(function(p1,p2){
                        //1代表升序，2代表降序
                        if(orderType === 2){
                            return p2.age-p1.age
                        }else{
                            return p1.age-p2.age
                        }
                    })
                    return fPersons
                }
            }
        })

    </script>

    <div id="demo">
        <h2>测试： v-for 遍历数组</h2>
        <ul>
            <li v-for="(p,index) in persons" :key="index">
                {{index}}---{{p.name}}---{{p.age}}
                ---<button @click="deleteP(index)">删除</button>
                ---<button @click="updateP(index,{name:'Cat',age:20})">更新</button>
            </li>

        </ul>

        <h2>测试： v-for 遍历对象</h2>
        <ul>
            <li v-for="(value,key) in persons[1]" :key="key">
                {{value}}---{{key}}
            </li>

            <li></li>
            <li></li>
        </ul>

    </div>

    <script type="text/javascript">
        //vue本身只是监视了persons 的改变，没有监视数组内部数据的改变
        //
        new Vue({
            el:'#demo',
            data: {
                persons:[
                    {name:'Tom',age:18},
                    {name:'Jack',age:16},
                    {name:'Bob',age:19},
                    {name:'Rose',age:17}
                ]
            },
            methods:{
                deleteP(index){
                    //删除persons中指定index的p
                    this.persons.splice(index,1)
                },
                updateP(index,newP){
//                    this.persons[index]=newP
//                      并没有改变persons本身，数组内部发生了变化，但并没有调用变异方法，vue不会更新界面

//                      this.persons = []
                    this.persons.splice(index,1,newP)
                }
            }
        })

        var obj = {}
        obj.t =1;

    </script>

</body>
</html>